package book.wirte;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.fasterxml.jackson.databind.ObjectMapper;

import book.dao.impl.BookDaoImpl;
import book.pojo.Book;

public class BookWriteFile {

	private static BookDaoImpl bdi;
	public static void main(String[] args) throws IOException, JMSException {
		// TODO Auto-generated method stub
		ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://10.222.29.152:61616");
		Destination queueDestination=new ActiveMQQueue("books");
		Connection connection =factory.createConnection();
		connection.start();
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageProducer mp=session.createProducer(queueDestination);
		
		ObjectMapper oMapper=new ObjectMapper();
		
		int count=0;
		Book temp=new Book();
		Long startLong=System.currentTimeMillis();
		for(int i=0;i<10000;i++)
		{
			temp.setId(i);
			temp.setName("name"+i);
			temp.setPub_date(new Date(System.currentTimeMillis()+i*100000));
			temp.setIs_new((i%2)==0?true:false);
			temp.setAuthor("how Knows");
			temp.setPrice(Math.random()*1000);
			temp.setPublisher("Peking");
			TextMessage msg=session.createTextMessage(oMapper.writeValueAsString(temp));
			mp.send(msg);
			count++;
		}
		Long endLong=System.currentTimeMillis();
		System.out.println("has wirte "+count+" books...");
		System.out.println(endLong-startLong);
		
	}

}
